package com.ccys.cms.controller.consultation;


import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.consultation.BookingConsultNoteService;
import com.ccys.cms.service.consultation.PDFService;
import com.ccys.cms.service.userManagement.TblMemberWebService;
import com.ccys.common.util.HtmlUtils;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.entity.TblBookingMemberInfo;
import com.ccys.db.entity.TblBookingMemberInfoRefreshLog;
import com.ccys.db.entity.TblMember;
import com.ccys.db.mapper.TblBookingConsultNoteMapper;
import com.ccys.db.mapper.TblBookingLogMapper;
import com.ccys.db.mapper.TblBookingMemberInfoMapper;
import com.ccys.db.mapper.TblBookingMemberInfoRefreshLogMapper;
import com.ccys.db.vo.consultation.ConsultationBasicsDetailVo;
import com.ccys.db.vo.consultation.ReferralLetterPDFVo;
import com.ccys.db.vo.consultation.TimeLineVo;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.ccys.db.cache.TranslationUtil.getContentLastLanguage;

@RestController
@Api(tags = "问诊订单列表详情Basics")
@RequestMapping("consultation")
@Slf4j
public class ConsultationDetailBasicsController {


    @Resource
    private TblBookingMemberInfoMapper memberInfoMapper;

    @Resource
    private TblMemberWebService memberService;

    @Resource
    private TblBookingMemberInfoRefreshLogMapper infoLogMapper;

    @Resource
    private TblBookingLogMapper logMapper;


    @Resource
    private BookingConsultNoteService consultNoteService;

    @Resource
    private TblBookingConsultNoteMapper consultNoteMapper;


    @Resource
    private PDFService pdfService;

    @GetMapping("refreshMemberInfo")
    @ApiOperation(value = "刷新")
    @Transactional
    public ResponseResult refreshMemberInfo(String bookingId) {
        // 查 tbl_member的数据用做new_xxxx字段的数据，
        // 查询tbl_booking_member_info的数据当做 old_xxxx的数据
        // 新增到 tbl_booking_member_info_refresh_log表里面。
        Example example = new Example(TblBookingMemberInfo.class);
        example.createCriteria().andEqualTo("bookingId", bookingId).andEqualTo("delFlag", 0);
        TblBookingMemberInfo oldInfo = memberInfoMapper.selectOneByExample(example);
        if (ObjectUtil.isNotEmpty(oldInfo)) {
            String memberId = oldInfo.getMemberId();
            TblMember newInfo = memberService.findOneByLimit(TblMember.class, new ArrayList<String>() {{
                add("del_flag = 0");
                add(" id = '" + memberId + "'");
            }});

            TblBookingMemberInfoRefreshLog infoLog = new TblBookingMemberInfoRefreshLog();
            infoLog.setBookingId(bookingId);
            infoLog.setMemberId(memberId);
            String childId = oldInfo.getChildId();
            infoLog.setChildId(childId);
            infoLog.setOldMemberFirstName(oldInfo.getMemberFirstName());
            infoLog.setOldMemberLastName(oldInfo.getMemberLastName());
            infoLog.setOldMemberNricFin(oldInfo.getMemberNricFin());
            infoLog.setOldMemberDob(oldInfo.getMemberDob());
            infoLog.setOldMemberEmail(oldInfo.getMemberEmail());
            infoLog.setOldMemberGender(oldInfo.getMemberGender());
            infoLog.setOldMemberPhoneNumber(oldInfo.getMemberPhoneNumber());

            infoLog.setNewMemberFirstName(newInfo.getFirstName());
            infoLog.setNewMemberLastName(newInfo.getLastName());
            infoLog.setNewMemberNricFin(newInfo.getNricFin());
            infoLog.setNewMemberDob(newInfo.getDob());
            infoLog.setNewMemberEmail(newInfo.getEmail());
            infoLog.setNewMemberGender(newInfo.getGender());
            infoLog.setNewMemberPhoneNumber(newInfo.getPhone());

            if (StringUtils.isNotEmpty(childId)) {

                infoLog.setOldChildFirstName(oldInfo.getChildFirstName());
                infoLog.setOldChildLastName(oldInfo.getChildLastName());
                infoLog.setOldChildNricFin(oldInfo.getChildNricFin());
                infoLog.setOldChildDob(oldInfo.getChildDob());
                infoLog.setOldChildEmail(oldInfo.getChildEmail());
                infoLog.setOldChildGender(oldInfo.getChildGender());
                infoLog.setOldChildPhoneNumber(oldInfo.getChildPhoneNumber());

                TblMember childInfo = memberService.findOneByLimit(TblMember.class, new ArrayList<String>() {{
                    add("del_flag = 0");
                    add(" id = '" + childId + "'");
                }});

                if (ObjectUtil.isNotEmpty(childInfo)) {
                    infoLog.setNewChildFirstName(childInfo.getFirstName());
                    infoLog.setNewChildLastName(childInfo.getLastName());
                    infoLog.setNewChildNricFin(childInfo.getNricFin());
                    infoLog.setNewChildDob(childInfo.getDob());
                    infoLog.setNewChildEmail(childInfo.getEmail());
                    infoLog.setNewChildGender(childInfo.getGender());
                    infoLog.setNewChildPhoneNumber(childInfo.getPhone());
                }

            }

            int i = infoLogMapper.insert(infoLog);
            return ResponseResult.update(i);
        }
        // TODO: 2024/2/4 0004 刷新的时候“重新”生成pdf，上传到booking_image里面
        // 点击下载pdf的时候先从image里面找，没找到生成然后存到image里面
        return ResponseResult.success();
    }


    @GetMapping("getBasicsInfo")
    @ApiOperation(value = "查询basics详情", notes = "同时可以用作consultation List edit查询详情的接口")
    public ResponseResult<ConsultationBasicsDetailVo, Object> getBasicsInfo(String bookingId) {
        return ResponseResult.success(consultNoteService.getBasicsInfo(bookingId));
    }


    @GetMapping("getTimeLine")
    @ApiOperation("查询timeLine")
    public ResponseResult<List<TimeLineVo>, Object> getTimeLine(String bookingId) {
        List<TimeLineVo> timeLine = consultNoteMapper.getTimeLine(bookingId);
        return ResponseResult.success(timeLine);
    }

    @ApiOperation("下载referral letter pdf文件")
    @GetMapping(value = "/downloadPDF/referralLetter")
    public ResponseResult downloadReferralLetter(HttpServletResponse response, String filename) {

       String directory = "Consultation/ReferralLetter/";
       pdfService.downloadPDF(response,filename,directory);
       return ResponseResult.success("ok");
    }


    @ApiOperation("下载 memo pdf 文件")
    @GetMapping("/downloadPDF/memo")
    public ResponseResult downloadMemo(HttpServletResponse response, String filename) {
        //Consultation/Memo/
        String directory = "Consultation/Memo/";
        pdfService.downloadPDF(response, filename, directory);
        return ResponseResult.success("ok");
    }
}
